package cn.kgc.controller;

import cn.kgc.base.SystemConstant;
import cn.kgc.excpetion.GraceException;
import cn.kgc.grace.result.GraceJSONResult;
import cn.kgc.grace.result.ResponseStatusEnum;
import cn.kgc.util.FileManager;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author wangyue
 * @version 1.0
 */
@RestController
@RequestMapping("/file")
@Api(tags = "文件管理模块")
@Slf4j
public class FileController {
    @Resource
    private FileManager fileManager;


    @PostMapping("/upload/chatPhoto")
    public  GraceJSONResult uploadChatPhoto(@RequestParam("multipartFile") MultipartFile multipartFile){
        if(multipartFile.isEmpty()){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        String filePath= null;
        try {
            filePath = fileManager.uploadFile(multipartFile, SystemConstant.CHAT_PHOTO_PATH);
        } catch (Exception e) {
            //如果上传文件失败抛出异常
            GraceException.display(ResponseStatusEnum.FILE_UPLOAD_FAILD);
            return GraceJSONResult.error();
        }
        return GraceJSONResult.ok(filePath);
    }

    @PostMapping("/upload/chatVideo")
    public  GraceJSONResult uploadChatVideo(@RequestParam("multipartFile") MultipartFile multipartFile){
        if(multipartFile.isEmpty()){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        String filePath= null;
        try {
            filePath = fileManager.uploadFile(multipartFile, SystemConstant.CHAT_VIDEO_PATH);
        } catch (Exception e) {
            //如果上传文件失败抛出异常
            GraceException.display(ResponseStatusEnum.FILE_UPLOAD_FAILD);
            return GraceJSONResult.error();
        }
        return GraceJSONResult.ok(filePath);
    }



    @PostMapping("/upload/chatAudio")
    public  GraceJSONResult uploadChatAudio(@RequestParam("multipartFile") MultipartFile multipartFile){
        if(multipartFile.isEmpty()){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        String filePath= null;
        try {
            filePath = fileManager.uploadFile(multipartFile, SystemConstant.CHAT_AUDIO_PATH);
        } catch (Exception e) {
            //如果上传文件失败抛出异常
            GraceException.display(ResponseStatusEnum.FILE_UPLOAD_FAILD);
            return GraceJSONResult.error();
        }
        return GraceJSONResult.ok(filePath);
    }

    @PostMapping("/upload/face")
    public GraceJSONResult uploadFace(@RequestParam("multipartFile") MultipartFile multipartFile){
        //判断文件是否为空
        if(multipartFile.isEmpty()){
           return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        try {
            //上传文件
            String filePath= fileManager.uploadFile(multipartFile,SystemConstant.FACE_PATH);
            //返回文件保存的地址
            return GraceJSONResult.ok(filePath);
        } catch (Exception e) {
            GraceException.display(ResponseStatusEnum.FILE_UPLOAD_FAILD);
            return GraceJSONResult.error();
        }
    }

    @PostMapping("/upload/logo")
    public GraceJSONResult uploadLogo(@RequestParam("multipartFile") MultipartFile multipartFile){
        if(multipartFile.isEmpty()){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        try {
            String filePath= fileManager.uploadFile(multipartFile,SystemConstant.LOGO_PATH);
            return GraceJSONResult.ok(filePath);
        } catch (Exception e) {
            GraceException.display(ResponseStatusEnum.FILE_UPLOAD_FAILD);
            return GraceJSONResult.error();
        }
    }

    @PostMapping("/upload/bizLicense")
    public GraceJSONResult uploadBizLicense(@RequestParam("multipartFile") MultipartFile multipartFile){
        if(multipartFile.isEmpty()){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        try {
            String filePath= fileManager.uploadFile(multipartFile,SystemConstant.BIZ_LICENSE_PATH);
            return GraceJSONResult.ok(filePath);
        } catch (Exception e) {
            GraceException.display(ResponseStatusEnum.FILE_UPLOAD_FAILD);
            return GraceJSONResult.error();
        }
    }

    @PostMapping("/upload/authLetter")
    public GraceJSONResult uploadAuthLetter(@RequestParam("multipartFile") MultipartFile multipartFile){
        if(multipartFile.isEmpty()){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        try {
            String filePath= fileManager.uploadFile(multipartFile,SystemConstant.AUTH_LETTER_PATH);
            return GraceJSONResult.ok(filePath);
        } catch (Exception e) {
            GraceException.display(ResponseStatusEnum.FILE_UPLOAD_FAILD);
            return GraceJSONResult.error();
        }
    }
}